//
//  ST_mutilLineBarrageView.swift
//  BTVideo
//
//  Created by DouLei on 2019/9/12.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class ST_mutilLineBarrageView: UIView {

//    MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let gap = 9.cgFloat
        
        addSubview(barrageView1)
        addSubview(barrageView2)
        addSubview(barrageView3)
        addSubview(barrageView4)
        barrageView1.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(barrageLabelHeight)
        }
        
        barrageView2.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(barrageView1.snp.bottom).offset(gap)
            make.height.equalTo(barrageView1)
        }
        
        barrageView3.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(barrageView2.snp.bottom).offset(gap)
            make.height.equalTo(barrageView1)
        }
        
        barrageView4.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(barrageView3.snp.bottom).offset(gap)
            make.height.equalTo(barrageView1)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var  barrage_views = [barrageView1,barrageView2,barrageView3,barrageView4]
    
    lazy var barrageView1:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView2:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView3:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView4:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
//    MARK: - SetUp Data
    var data_array = [ST_FWBarrageItemModel](){
        didSet{
            
            var array1 = [ST_FWBarrageItemModel]()
            var array2 = [ST_FWBarrageItemModel]()
            var array3 = [ST_FWBarrageItemModel]()
            var array4 = [ST_FWBarrageItemModel]()

            for (idx,item) in data_array.enumerated() {

                if idx%4 == 0{
                    array1.append(item)
                }else if idx%4 == 1{
                    array2.append(item)
                }else if idx%4 == 2{
                    array3.append(item)
                }else{
                    array4.append(item)
                }
            }
            
//            barrageView1.data_array = data_array
            barrageView1.data_array = array1
            barrageView2.data_array = array2
            barrageView3.data_array = array3
            barrageView4.data_array = array4
            
            
        }
    }
    
    

    //    MARK: - Public Method
    func play(){
        
        for item in barrage_views{
            item.play()
        }
        
//        barrageView1.play()
    }
    
    func stop(){
        for item in barrage_views{
            item.stop()
        }
        
//        barrageView1.stop()
    }
    
    func out_side_stop()  {
        for item in barrage_views{
            item.out_side_stop()
        }
//        barrageView1.out_side_stop()
    }
        
        /**
         暂定 自己发弹幕调用
         */
    func insert(barrage:ST_BarrageModel)  {
        
        barrage_views[Int.random(in: 0...3)].insert(barrage: barrage)
//        barrage_views[0].insert(barrage: barrage)
    }
    
}






//MARK: - 10 条
class ST_TenLineBarrageView: UIView {

    static let height = barrageLabelHeight * 10 + 19 * 9
//    MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let gap = 19.cgFloat
        
        addSubview(barrageView1)
        addSubview(barrageView2)
        addSubview(barrageView3)
        addSubview(barrageView4)
        addSubview(barrageView5)
        addSubview(barrageView6)
        addSubview(barrageView7)
        addSubview(barrageView8)
        addSubview(barrageView9)
        addSubview(barrageView10)
        
        var lastItem:ST_OneLineBarrageView?
        
        barrage_views.forEach { (body) in
        
            if let last = lastItem{
                
                body.snp.makeConstraints { (make) in
                    make.left.right.equalToSuperview()
                    make.top.equalTo(last.snp.bottom).offset(gap)
                    make.height.equalTo(barrageLabelHeight)
                }
            }else{
                
                body.snp.makeConstraints { (make) in
                    make.left.right.top.equalToSuperview()
                    make.height.equalTo(barrageLabelHeight)
                }
            }
            
            lastItem = body
        }
        
        barrageView1.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(barrageLabelHeight)
        }
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 循环播放弹幕
    var is_loop_play = true{
        didSet{
            barrage_views.forEach({ $0.is_loop_play = is_loop_play })
        }
    }
    
    lazy var  barrage_views = [barrageView1,barrageView2,barrageView3,barrageView4,barrageView5,barrageView6,barrageView7,barrageView8,barrageView9,barrageView10]
    
    lazy var barrageView1:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView2:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView3:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView4:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView5:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView6:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView7:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView8:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView9:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    lazy var barrageView10:ST_OneLineBarrageView = {
        let control = ST_OneLineBarrageView()
        return control
    }()
    
    fileprivate let range = [0...10,11...20,21...30,31...40,41...50,51...60,61...80]
    
//    MARK: - SetUp Data
    var data_array = [ST_FWBarrageItemModel](){
        didSet{

            
            var sumCount = 2
            
            let idx = range.firstIndex { (rg) -> Bool in
                return rg.contains(data_array.count)
            }
            
            if let index = idx?.uInt{
                sumCount += Int(index)
            }
            
            var count = data_array.count/sumCount
            
            if data_array.count == 0{
                return
            }else if count < 1{
                count = 1
            }
            
            let array = data_array.chunked(into: count)
            
            for (idx,itemView) in barrage_views.enumerated() {
                
                if let a = array[safe:idx]{
                    itemView.data_array = a
                }
            }
            
        }
    }
    
    

    //    MARK: - Public Method
    func play(){
        
        for item in barrage_views{
            item.play()
        }
        
//        barrageView1.play()
    }
    
    func stop(){
        for item in barrage_views{
            item.stop()
        }
        
//        barrageView1.stop()
    }
    
    func out_side_stop()  {
        for item in barrage_views{
            item.out_side_stop()
        }
//        barrageView1.out_side_stop()
    }
        
        /**
         暂定 自己发弹幕调用
         */
    func insert(barrage:ST_BarrageModel)  {
        
        barrage_views[Int.random(in: 0..<4)].insert(barrage: barrage)
//        barrage_views[0].insert(barrage: barrage)
    }
    
}




extension Array {
    func chunked(into size: Int) -> [[Element]] {
        return stride(from: 0, to: count, by: size).map {
            Array(self[$0 ..< Swift.min($0 + size, count)])
        }
    }
}
